import { Component,OnInit } from '@angular/core';
// 引入Parse第三方库
import * as Parse from "parse"
(Parse as any).serverURL = "http://metapunk.cn:9999/parse"
Parse.initialize("dev")

@Component({
  selector: 'app-page-chef',
  templateUrl: './page-chef.component.html',
  styleUrls: ['./page-chef.component.scss']
})
export class PageChefComponent implements OnInit{
  // 导航栏
    tagList = ["早餐",  "午餐",  "晚餐",  "夜宵",  "下午茶",  "川菜","湘菜"]
  //搜索框搜索食物名称
    name: string = '';
    onSearch(name:string){
       this.showName(name)
       }
    searchByOrder(ev:any,key?:string,type?:string){
      let value = ev.srcElement.options[ev.srcElement.selectedIndex].value
      if(value){
        key = value.split("-")[0]
        type = value.split("-")[1]
      }
      this.showSort(this.currentCate,{
        key:key,type:type
      })
    }
  //从数据库搜索搜索框的内容并渲染
     async showName(item:any){
      console.log(item);
      let query = new Parse.Query("Goods")
      query.equalTo("title",item) // 参考：https://docs.parseplatform.org/js/guide/#basic-queries
      let list = await query.find()
      this.dishesList = list 
     }
    ngOnInit():void{
      //默认显示早餐
        this.showSort("早餐")
      }
  //筛选相关种类食物
    dishesList:any=[]
    currentCate:string = "早餐"
    async showSort(item:any,orderOptions?:{
      key:any,type:any
    }){
      console.log(item);
      console.log(orderOptions)
      let query = new Parse.Query("Goods")
      this.currentCate = item
      query.equalTo("cate",item) // 参考：https://docs.parseplatform.org/js/guide/#basic-queries

      if(orderOptions){
        if(orderOptions.type=="asc"){
          query.ascending(orderOptions.key)
        }
        if(orderOptions.type=="desc"){
          query.descending(orderOptions.key)
        }
      }

      // console.log(query.toJSON())
      let list = await query.find()
      this.dishesList = list   
    }
}
